﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.common;

namespace rock.UI
{
    public partial class FormCfgCode : UserControl
    {
       private DalCFG_CODE dal = new DalCFG_CODE();
       List<Info> InfoList = new List<Info>();
       String selectType = "";
        
    


       public FormCfgCode()
        {
            InitializeComponent();

            Info info1 = new Info() { Id = "房屋朝向", Name = "房屋朝向" };
            InfoList.Add(info1);

            Info info2 = new Info() { Id = "押金类型", Name = "押金类型" };
            InfoList.Add(info2);

            Info info3 = new Info() { Id = "楼宇类型", Name = "楼宇类型" };
            InfoList.Add(info3);

            Info info4 = new Info() { Id = "开发商", Name = "开发商" };
            InfoList.Add(info4);

            Info info5 = new Info() { Id = "户型", Name = "户型" };
            InfoList.Add(info5);

            Info info6 = new Info() { Id = "设备", Name = "设备" };
            InfoList.Add(info6);

            this.userControlCommonListHeader1.addClick = addClick;
            this.userControlCommonListHeader1.updateClick = updateClick;
            this.userControlCommonListHeader1.deleteClick = deleteClick;
            //this.userControlCommonListHeader1.selectClick = refreshData;
            this.userControlCommonListHeader1.searchClick = refreshData;
            this.Dock = System.Windows.Forms.DockStyle.Fill;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
        }

        private void CfgCode_Load(object sender, EventArgs e)
        {
            
           
            refreshData();
          //  this.listBox1.SelectedIndex = 0;
        }

        private class Info
        {
            public string Id { get; set; }

            public string Name { get; set; }
        }
        
        
        //刷新数据
        public void refreshData() {

            this.listBox1.DisplayMember = "Id";
            this.listBox1.ValueMember = "Name";
            this.listBox1.DataSource = InfoList;
            selectType = (String)this.listBox1.SelectedValue;
            DataSet ds = dal.GetList("TYPE = '" + selectType + "' AND CODE_VALUE LIKE '%"+this.userControlCommonListHeader1.textBox1.Text+"%'");
            this.dataGridView1.DataSource = ds.Tables[0];
        }
      

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            updateClick();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //刷新数据
            refreshData();
        }

        


        private void addClick()
        {
            FormCfgCodeValue fcv = new FormCfgCodeValue();


            selectType = (String)this.listBox1.SelectedValue;
            fcv._MCC.TYPE = selectType;
            //注册代理
            fcv.oneRefreshParentData = refreshData;
            fcv.ShowDialog(this);
        }
        private void updateClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                int row = this.dataGridView1.CurrentRow.Index;
                int col = this.dataGridView1.CurrentCell.ColumnIndex;
                // String value = this.dataGridView1.SelectedRows[row].Cells["CODE_VALUE"].Value.ToString();
                String value = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["CODE_VALUE"].Value.ToString();
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormCfgCodeValue fcv = new FormCfgCodeValue();

                selectType = (String)this.listBox1.SelectedValue;
                fcv._MCC.TYPE = selectType;
                fcv._MCC.CODE_VALUE = value;
                fcv._MCC.SEQID = Convert.ToInt32(seqid);
                //注册代理
                fcv.oneRefreshParentData = refreshData;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新的代码类型.", "提示");
            }
        }
        private void deleteClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {


                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        dal.Delete(seqidInt);
                        //刷新数据
                        refreshData();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }
         

        

    }
}
